package com.wwp.tbshop.activity.orderlist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wwp.tbshop.R;
import com.wwp.tbshop.dagger2.DaggerOrderDetailActivityComponent;
import com.wwp.tbshop.dagger2.OrderDetailAvtivityModule;
import com.wwp.tbshop.model.bean.OrderListItemDetailBean;
import com.wwp.tbshop.model.bean.OrderListItemDetailBean.ProductListBean;
import com.wwp.tbshop.presenter.OrderDetailOperation;
import com.wwp.tbshop.util.OrderDeliveryType;
import com.wwp.tbshop.util.OrderTimeFormatter;
import com.wwp.tbshop.util.PaymentTypeFormatter;
import com.wwp.tbshop.util.PriceFormater;
import com.wwp.tbshop.view.adapter.ProductListAdapter;
import com.wwp.tbshop.view.custom.ProductListView;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by user on 2017/6/16.
 */

public class OrderDetailActivity extends Activity {


    private static final String TAG = "OrderDetailActivity";
    @Inject
    OrderDetailOperation mDetailOperation;

    @Bind(R.id.order_addressInfo)
    TextView mOrderAddressInfo;
    @Bind(R.id.addressInfo_id)
    TextView mAddressInfoId;
    @Bind(R.id.addressInfo_name)
    TextView mAddressInfoName;
    @Bind(R.id.addressInfo_address)
    TextView mAddressInfoAddress;
    @Bind(R.id.order_info)
    TextView mOrderInfo;
    @Bind(R.id.order_info_status)
    TextView mOrderInfoStatus;
    @Bind(R.id.order_info_deli_type)
    TextView mOrderInfoDeliType;
    @Bind(R.id.order_info_pay_type)
    TextView mOrderInfoPayType;
    @Bind(R.id.order_info_time)
    TextView mOrderInfoTime;
    @Bind(R.id.order_info_invo)
    TextView mOrderInfoInvo;
    @Bind(R.id.order_info_invo_title)
    TextView mOrderInfoInvoTitle;
    @Bind(R.id.order_info_invo_content)
    TextView mOrderInfoInvoContent;
    @Bind(R.id.order_info_deli_require)
    TextView mOrderInfoDeliRequire;
    @Bind(R.id.goodslist)
    TextView mGoodslist;
    @Bind(R.id.goodslist_price_total)
    TextView mGoodslistPriceTotal;
    @Bind(R.id.goodslist_deli_fee)
    TextView mGoodslistDeliFee;
    @Bind(R.id.goodslist_prom)
    TextView mGoodslistProm;
    @Bind(R.id.goodslist_payment_price)
    TextView mGoodslistPaymentPrice;
    @Bind(R.id.order_detail)
    LinearLayout mOrderDetail;
    @Bind(R.id.order_cancel)
    Button mOrderCancel;
    @Bind(R.id.cancel_action)
    LinearLayout mCancelAction;
    @Bind(R.id.products_list)
    ProductListView mProductsList;
    private int mUserId;
    private int mOrderId;
    private int mListType;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);

       DaggerOrderDetailActivityComponent.builder().orderDetailAvtivityModule(new OrderDetailAvtivityModule(this)).build().in(this);

        Intent intent = getIntent();
        mUserId = intent.getIntExtra("userId", 0);
        mOrderId = intent.getIntExtra("orderId", 0);
        mListType = intent.getIntExtra("listType", 0);

        Log.e(TAG, "onCreate: orderId=" + mOrderId);
        Log.e(TAG, "onCreate: mListType=" + mListType);
        mDetailOperation.getOrderDetail(mUserId, mOrderId);
    }


    public void showOrderDetail(OrderListItemDetailBean orderListItemDetailBean) {
        if (mListType == 3) {
            mOrderCancel.setVisibility(View.GONE);
        } else {
            mOrderCancel.setVisibility(View.VISIBLE);
        }
        if (orderListItemDetailBean.response.equals("error")) {
            Toast.makeText(this, orderListItemDetailBean.error, Toast.LENGTH_SHORT).show();
            return;
        } else {
            mOrderDetail.setVisibility(View.VISIBLE);
        }


        OrderListItemDetailBean.AddressInfoBean addressInfoBean = orderListItemDetailBean.addressInfo;
        OrderListItemDetailBean.OrderInfoBean orderInfoBean = orderListItemDetailBean.orderInfo;
        OrderListItemDetailBean.PaymentInfoBean paymentInfoBean = orderListItemDetailBean.paymentInfo;
        OrderListItemDetailBean.InvoiceInfoBean invoiceInfoBean = orderListItemDetailBean.invoiceInfo;
        List<ProductListBean> productList;
        productList = orderListItemDetailBean.productList;

        mAddressInfoId.setText(mAddressInfoId.getText() + orderInfoBean.orderId);
        mAddressInfoAddress.setText(mAddressInfoAddress.getText() + addressInfoBean.addressArea + " " + addressInfoBean.addressDetail);
        mAddressInfoName.setText(mAddressInfoName.getText() + addressInfoBean.name);


        mOrderInfoStatus.setText(mOrderInfoStatus.getText() + orderInfoBean.status);
        mOrderInfoDeliType.setText(mOrderInfoDeliType.getText() + OrderDeliveryType.format(Integer.parseInt(orderListItemDetailBean.deliveryInfo.type)));
        mOrderInfoPayType.setText(mOrderInfoPayType.getText() + "" + PaymentTypeFormatter.format(paymentInfoBean.type));
        mOrderInfoTime.setText(mOrderInfoTime.getText() + "" + OrderTimeFormatter.format(orderInfoBean.time));
        mOrderInfoInvoContent.setText(mOrderInfoInvoContent.getText() + "" + invoiceInfoBean.invoiceContent);
        mOrderInfoInvoTitle.setText(mOrderInfoInvoTitle.getText() + "" + invoiceInfoBean.invoiceTitle);

        if(TextUtils.isEmpty(invoiceInfoBean.invoiceTitle)){
            mOrderInfoInvo.setText(mOrderInfoInvo.getText()+"否");
        }else{
            mOrderInfoInvo.setText(mOrderInfoInvo.getText()+"是");
        }



        OrderListItemDetailBean.CheckoutAddupBean checkoutAddupBean = orderListItemDetailBean.checkoutAddup;
        mGoodslistPriceTotal.setText(mGoodslistPriceTotal.getText() + "" + PriceFormater.format(checkoutAddupBean.totalPrice));
        mGoodslistDeliFee.setText(mGoodslistDeliFee.getText() + "" + PriceFormater.format(checkoutAddupBean.freight));
        mGoodslistPaymentPrice.setText(mGoodslistPaymentPrice.getText()+PriceFormater.format(checkoutAddupBean.totalPrice+checkoutAddupBean.freight));

        mProductsList.setAdapter(new ProductListAdapter(this,productList));


    }

    @OnClick(R.id.order_cancel)
    public void onClick() {
        mDetailOperation.requestCancelOrder(mUserId, mOrderId);
        finish();
    }
}

